package com.pig4cloud.pig.module.controller.titleMenu;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.OkHttpClientUtil;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.titleMenu.TitleMenu;
import com.pig4cloud.pig.module.service.titleMenu.ITitleMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 首页菜单Controller
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Validated
@Api(value = "首页菜单控制器", tags = {"首页菜单管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/titleMenu")
public class TitleMenuController {

    private final ITitleMenuService titleMenuService;

    @Value("${ruoyi.imgUrl}")
	private String imgUrl;

//	@Value("${ruoyi.imgUrlOss}")
//	private String imgUrlOss;

    /**
     * 查询首页菜单列表
     */
    @ApiOperation("查询首页菜单列表")
    @PreAuthorize("@pms.hasPermission('module:menus:list')")
    @GetMapping("/list")
    public TableDataInfo<TitleMenu> list(@Validated TitleMenu titleMenu) {
		QueryWrapper<TitleMenu> qw = new QueryWrapper<>();
		if(titleMenu.getIsShowFirst() !=null){
			if(titleMenu.getIsShowFirst() == 1){
				qw.eq("is_show_first",titleMenu.getIsShowFirst());
			}
		}
		if(titleMenu.getCategoryType() !=null){
			qw.eq("category_type",titleMenu.getCategoryType());
		}
		if(titleMenu.getMenuType() !=null){
			qw.eq("menu_type",titleMenu.getMenuType());
		}
		qw.orderByAsc("sort");
		TableDataInfo<TitleMenu> td = PageUtils.buildDataInfo(titleMenuService.page(PageUtils.buildPage(),qw));
		List<TitleMenu> list = td.getData();
		if(CollUtil.isNotEmpty(list)){
			list.forEach(titleMenu1 -> {
                if(StrUtil.isNotEmpty(titleMenu1.getLinkLogo())){
                    if(StrUtil.isNotEmpty(titleMenu1.getLinkLogo()) && titleMenu1.getLinkLogo().indexOf("http") < 0){
                        titleMenu1.setImgPath(imgUrl + titleMenu1.getLinkLogo());
                    }else{
                        titleMenu1.setImgPath(titleMenu1.getLinkLogo());
                    }
                }else{
                    titleMenu1.setImgPath("");
                }
			});
		}
        return td;
    }

    /**
     * 获取首页菜单详细信息
     */
    @ApiOperation("获取首页菜单详细信息")
    @PreAuthorize("@pms.hasPermission('module:menus:query')")
    @GetMapping("/{id}")
    public R<TitleMenu> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
    	TitleMenu titleMenu = titleMenuService.getById(id);
    	if(titleMenu != null) {
    		if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                if(StrUtil.isNotEmpty(titleMenu.getLinkLogo()) && titleMenu.getLinkLogo().indexOf("http") < 0){
                    titleMenu.setImgPath(imgUrl + titleMenu.getLinkLogo());
                }else{
                    titleMenu.setImgPath(titleMenu.getLinkLogo());
                }
			}else{
				titleMenu.setImgPath("");
			}
		}
        return R.ok(titleMenu);
    }

    /**
     * 新增首页菜单
     */
    @ApiOperation("新增首页菜单")
    @PreAuthorize("@pms.hasPermission('module:menus:add')")
    @SysLog("首页菜单")

    @PostMapping()
    public R<Void> add(@Validated @RequestBody TitleMenu titleMenu) {
    	boolean bl = titleMenuService.save(titleMenu);
		if(bl){
			OkHttpClientUtil.getInstance().getData("https://api.sdyuedou.com/ydapi/redis/reset/menu/" + titleMenu.getMenuType());
			OkHttpClientUtil.getInstance().getData("https://api.sdyuedou.com/ydapi/redis/reset/title/menu/" + titleMenu.getCategoryType());
		}
        return R.okByRow(bl ? 1 : 0);
    }

    /**
     * 修改首页菜单
     */
    @ApiOperation("修改首页菜单")
    @PreAuthorize("@pms.hasPermission('module:menus:edit')")
    @SysLog("首页菜单")

    @PutMapping()
    public R<Void> edit(@Validated @RequestBody TitleMenu titleMenu) {
		boolean bl = titleMenuService.updateById(titleMenu);
		if(bl){
			OkHttpClientUtil.getInstance().getData("https://api.sdyuedou.com/ydapi/redis/reset/menu/" + titleMenu.getMenuType());
			OkHttpClientUtil.getInstance().getData("https://api.sdyuedou.com/ydapi/redis/reset/title/menu/" + titleMenu.getCategoryType());
		}
        return R.okByRow(bl ? 1 : 0);
    }


	/**
	 * 修改抖音上下架状态
	 */
	@PreAuthorize("@pms.hasPermission('module:menus:edit')")
	@SysLog("修改抖音上下架状态")
	@PutMapping("/changeShowDyStatus")
	public R changeShowDyStatus(@RequestBody TitleMenu titleMenu) {
		return R.okByRow(titleMenuService.updateById(titleMenu));
	}

    /**
     * 删除首页菜单
     */
    @ApiOperation("删除首页菜单")
    @PreAuthorize("@pms.hasPermission('module:menus:remove')")
    @SysLog("首页菜单")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(titleMenuService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
